package com.zlp.partice;

import org.junit.Test;

import java.util.*;

/**
 * @author: zhulaiping
 * @time: 2021/12/26 21:23
 */
public class Day20211226 {

    @Test
    public void eeq(){
        containsNearbyDuplicate(new int[]{1,2,3,1,2,3},2);
    }

    public int removePalindromeSub(String s) {
        int r = s.length()-1,l = 0;
        while (l!=r){
            if(s.charAt(l++)!=s.charAt(r--)){
                return 2;
            }
        }
        return 1;
    }

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i=0;i<nums.length;i++){
            List<Integer> list = map.getOrDefault(nums[i], new ArrayList<>());
            list.add(i);
            map.put(i,list);
        }
        for (List<Integer> cur:map.values()){
            for (int i=0;i<cur.size()-1;i++){
                if(cur.get(i+1)-cur.get(i)<k){
                    return true;
                }
            }
        }
        return false;
    }

    public String[] findOcurrences(String text, String first, String second) {
        String[] split = text.split(" ");
        List<String> ret = new ArrayList<>();
        for (int i=0;i<split.length-2;i++){
            if(split[i].equals(first)&&split[i+1].equals(second)){
                ret.add(split[i+2]);
            }
        }
        return ret.toArray(new String[ret.size()]);
    }



    public int countQuadruplets(int[] nums) {
        int ret = 0, len = nums.length;
        for (int i = 0; i < len - 3; i++) {
            for (int j = i + 1; j < len - 2; j++) {
                for (int k = j + 1; k < len - 1; k++) {
                    for (int m = k+1; m < len; m++) {
                        if (nums[i] + nums[j] + nums[k] == nums[m]) {
                            ret++;
                            System.out.println(nums[i]+"+"+nums[j]+"+"+nums[k]+"="+nums[m]);
                        }
                    }
                }
            }
        }
        return ret;
    }

    public boolean checkPerfectNumber(int num) {
        Set<Integer> set = new HashSet<>();
        int ret = 1;
        for (int i=2;i<=Math.sqrt(num);i++){
            if(num%i==0&&!set.contains(i)){
                ret += i;
                ret += num / i;
                set.add(i);
            }
        }
        return num*2 == ret;
    }

    @Test
    public void ee(){
        System.out.println(isNStraightHand(new int[]{1,2,3,6,2,3,4,7,8},3));
    }
    public boolean isNStraightHand(int[] hand, int groupSize) {
        int len = hand.length;
        if (len % groupSize != 0) return false;
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int val : hand) {
            map.put(val, map.getOrDefault(val, 0) + 1);
        }
        while (!map.isEmpty()) {
            int cur = map.firstKey();
            for (int i = 0; i < groupSize; i++) {
                if (!map.containsKey(cur)) {
                    return false;
                } else if (map.get(cur) == 1) {
                    map.remove(cur);
                } else {
                    map.put(cur, map.get(cur) - 1);
                }
                cur++;
            }
        }
        return true;
    }

    public int[][] construct2DArray(int[] original, int m, int n) {
        if(original.length != m*n) return new int[][]{};
        int[][] ret = new int[m][n];
        int index = 0;
        for (int i=0;i<m;i++){
            for (int j=0;j<n;j++){
                ret[i][j] = original[index++];
            }
        }
        return ret;
    }

    public int lastRemaining(int n) {
        int head = 1,step = 1;
        boolean left = true;
        while (n>1){
            if(left || n%2==1) {
                head +=  step;
            }
            n >>= 1;
            step <<= 1;
            left = !left;
        }
        return head;
    }

    @Test
    public void qq(){
        String fileUrl = "https://cabcnoms.carlsberg.asia/group1/M00/00/00/CuWSB2Dd0siACeNFAACAtjGjHNY817.jpg";
        String url = fileUrl.substring(fileUrl.indexOf("group1"));
        String group = url.substring(0, url.indexOf("/"));
        String path = url.substring(url.indexOf("/") + 1);
        System.out.println(group);
        System.out.println(path);
    }

    public boolean increasingTriplet(int[] nums) {

        return false;
    }
    public int dominantIndex(int[] nums) {
        int max = -1,index = -1;
        for (int i=0;i<nums.length;i++){
            if(max<nums[i]){
                max = nums[i];
                index = i;
            }
        }
        for (int i=0;i<nums.length;i++){
            if(i!=index&&max<nums[i]*2){
                return -1;
            }
        }
        return index;
    }
    public int findMinDifference(List<String> timePoints) {
        int ret = 1440,len = timePoints.size();
        Collections.sort(timePoints);
        for (int i=0;i<len;i++){
            String[] curs = timePoints.get(i).split(":");
            String[] cure = timePoints.get((i+1)%len).split(":");
            int cursRet = Integer.parseInt(curs[0])*60+Integer.parseInt(curs[1]);
            int cureRet = Integer.parseInt(cure[0])*60+Integer.parseInt(cure[1]);
            ret = Math.min(ret,(cureRet-cursRet+1440)%1440);
        }
        return ret;
    }

    public int countGoodRectangles(int[][] rectangles) {
        int max = 0,ret = 0;
        for (int[] arr: rectangles){
            int cur = Math.min(arr[0], arr[1]);
            if(max<cur){
                max = cur;
                ret = 1;
            }else if(max == cur){
                ret++;
            }
        }
        return ret;
    }

    public String reversePrefix(String word, char ch) {
        char[] array = word.toCharArray();
        for (int i=0;i<array.length;i++){
            if(ch == array[i]){
                reverse(array,0,i);
                return new String(array);
            }
        }
        return word;
    }

    private void reverse(char[] array, int i, int j) {
        while (i<j){
            char ch = array[i];
            array[i] = array[j];
            array[j] = ch;
            i++;
            j--;
        }
    }
    public int sumOfUnique(int[] nums) {

        int ret = 0;
        int[] map = new int[101];
        for (int index:nums) map[index] ++;
        for (int i=0;i<101;i++){
            if(map[i] == 1){
                ret += i;
            }
        }
        return ret;
    }

    public List<String> simplifiedFractions(int n) {
        List<String> ret = new ArrayList<>();
        for (int i=1;i<n;i++){
            for (int j=i+1;j<=n;j++){
                if(isSuit(i,j)) continue;
                ret.add(i+"/"+j);
            }
        }
        return ret;
    }

    private boolean isSuit(int i, int j) {
        for (int k=2;k<=i/2;k++){
            if(i%k==0&&j%k==0){
                return false;
            }
        }
        return true;
    }
    @Test
            public void eee(){
        numEnclaves(new int[][]{{0,0,0,1,1,1,0,1,0,0},
                {1,1,0,0,0,1,0,1,1,1},
                {0,0,0,1,1,1,0,1,0,0},
                {0,1,1,0,0,0,1,0,1,0}});
    }
    public int numEnclaves(int[][] grid) {
        int row = grid.length,column = grid[0].length;
        for (int i=0;i<row;i++){
            for (int j=0;j<column;j++){
                if((i==0||j==0)&&grid[i][j] == 1){
                    dfs(i,j,grid);
                }
            }
        }
        int ret = 0;
        for (int i=0;i<row;i++){
            for (int j=0;j<column;j++){
                if(grid[i][j] == 1) ret++;
            }
        }
        return ret;
    }

    int[][] directs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
    private void dfs(int i, int j, int[][] grid) {
        if(i<0||i>=grid.length||j<0||j>=grid[0].length||grid[i][j]!=1) return;
        grid[i][j] = 0;
        for (int[] direct:directs){
            dfs(i+direct[0],j+direct[1],grid);
        }
    }

}
